package cn.wolfcode.service.impl;

import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.domain.MessageReply;
import cn.wolfcode.mapper.MessageBoardMapper;
import cn.wolfcode.mapper.MessageReplyMapper;
import cn.wolfcode.qo.MessageBoardQueryObject;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MessageBoardServiceImpl implements IMessageBoardService {
    @Autowired
    private MessageBoardMapper messageBoardMapper ;

    @Autowired
    private MessageReplyMapper messageReplyMapper;


    @Override
    public void insert(MessageBoard messageBoard) {
        messageBoard.setCreateTime(new Date());
        messageBoardMapper.insert(messageBoard);
    }

    @Override
    public void delete(Long id) {
        messageBoardMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(MessageBoard messageBoard) {
        messageBoardMapper.updateByPrimaryKey(messageBoard);
    }

    @Override
    public MessageBoard selectById(Long id) {
        MessageBoard messageBoard = messageBoardMapper.selectByPrimaryKey(id);
        return messageBoard ;
    }

    @Override
    public List<MessageBoard> selectAll() {
        List<MessageBoard> messageBoards = messageBoardMapper.selectAll();
        return messageBoards ;
    }

    @Override
    public PageInfo<MessageBoard> query(MessageBoardQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),"create_time desc");
        List<MessageBoard> messageBoards = messageBoardMapper.queryAll(qo);
        return new PageInfo<>(messageBoards);
    }

    @Override
    public List<MessageReply> selectByReplay(Long id) {
        List<MessageReply> messageReplies = messageReplyMapper.selectByMid(id);
        return messageReplies;
    }

    @Override
    public int selectCount(Long id) {
        int count = messageReplyMapper.selectCount(id);
        return count;
    }


}
